package com.cloudbroker.bcs.common.t2sdk.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hundsun.t2sdk.common.share.dataset.MapWriter;
import com.hundsun.t2sdk.interfaces.share.dataset.IDataset;

public class T2SdkUtil {
    
    public static IDataset toDataset(Object bean) {
        Map<String, Object> map = (JSONObject) JSONObject.toJSON(bean);
        return toDataset(map);
    }
    
    @SuppressWarnings("unchecked")
    public static IDataset toDataset(Map<String, Object> map) {
        MapWriter mw = new MapWriter();
        for (Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Map) {
                value = toDataset((Map<String, Object>) value);
            } else if (value instanceof Iterable) {
                value = toDataset((Iterable<Object>) value);
            }
            mw.put(key, value);
        }
        return mw.getDataset();
    }
    
    @SuppressWarnings({ "unchecked" })
    public static IDataset toDataset(Iterable<Object> iterable) {
        IDataset ds = null;
        for (Object element : iterable) {
            if (element instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) element;
                if (null == ds) {
                    ds = createDataset(map);
                } else {
                    for (Entry<String, Object> entry : map.entrySet()) {
                        String key = entry.getKey();
                        Object value = entry.getValue();
                        ds.appendRow();
                        ds.updateValue(key, value);
                    }
                }
            }
        }
        return ds;
    }
    
    public static IDataset createDataset(Map<String, Object> param) {
        MapWriter mw = new MapWriter();
        for (Entry<String, Object> entry : param.entrySet()) {
            mw.put(entry.getKey(), entry.getValue());
        }
        return mw.getDataset();
    }
    
    public static Object toMapOrListMap(IDataset dataset) {
        int rc = dataset.getRowCount();
        if (1 < rc) {
            return toListMap(dataset);
        } else {
            return toMap(dataset);
        }
    }
    
    public static Map<String, Object> toMap(IDataset dataset) {
        return readRow(dataset);
    }
    
    public static List<Map<String, Object>> toListMap(IDataset dataset) {
        int rc = dataset.getRowCount();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(rc);
        dataset.beforeFirst();
        while (dataset.hasNext()) {
            dataset.next();
            Map<String, Object> map = readRow(dataset);
            list.add(map);
        }
        return list;
    }
    
    private static Map<String, Object> readRow(IDataset dataset) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (dataset.getRowCount() == 0) {
            return map;
        }
        int lc = dataset.getColumnCount();
        for (int i = 1; i <= lc; i++) {
            String key = dataset.getColumnName(i);
            Object value = dataset.getValue(i);
            if (value instanceof IDataset) {
                value = toListMap((IDataset) value);
            }
            map.put(key, value);
        }
        return map;
    }
    
    public static <T> T toBean(IDataset dataset, Class<T> beanType) {
        Object object = toMapOrListMap(dataset);
        return JSON.parseObject(JSON.toJSONString(object), beanType);
    }
    
}
